# -*- coding: utf-8 -*-
# Author : hgl
# DATE :2021/1/26 11:07
# File : XmindToExcelTest.py

import xlwt
from xmindparser import xmind_to_dict


class XmindToExcelTest():

    def write_excel(self, xmind_file):
        '''生成excel文件函数'''
        # print(xmind_file)
        xm = xmind_to_dict(xmind_file)[0]['topic'] #读取Xmind数据
        workbook = xlwt.Workbook()
        worksheet = workbook.add_sheet(xm["title"], cell_overwrite_ok=True)     #创建一个工作表
        self.row0 = [ '需求名称','模块名称', '测试用例名称','前置条件', '执行步骤', '预期结果']
        # 生成第一行
        for i in range(0, len(self.row0)):      #循环遍历row0，把值写入excel作为列的title
            worksheet.write(0, i, self.row0[i])

        x = 1 #写入数据的当前行数

        for i in range(len(xm['topics'])):      #循环遍历获取xmind不同节点的值，然后写入到excel对应的字段里面
            test_module = xm['topics'][i]
            # print(test_module)
            # print(len(test_module['topics']))
            print(xm['topics'][i]['title'])
            for j in range(len(test_module['topics'])):
                test_suit = test_module['topics'][j]
                # print(json.dumps(test_suit, ensure_ascii=False))
                # print(len(test_suit['topics']))
                print(test_module['topics'][j]['title'])
                for k in range(len(test_suit['topics'])):
                    test_case = test_suit['topics'][k]
                    # print(json.dumps(test_case, ensure_ascii=False))
                    # print(len(test_case))
                    print(test_suit['topics'][k]['title'])
                    for m in range(len(test_case['topics'])):
                        test_case_t = test_case['topics'][m]
                        # print(test_case_t)
                        # print(len(test_case_t))
                        print(test_case['topics'][m]['title'])
                        for n in range(len(test_case_t['topics'])):
                            test_case_q = test_case_t['topics'][n]
                            # print(test_case_q)
                            print(test_case_t['topics'][n]['title'])
                            for t in range(len(test_case_q['topics'])):
                                test_case_b = test_case_q['topics'][t]
                                # print(test_case_b)
                                print(test_case_q['topics'][t]['title'])
                                worksheet.write(x, 0, xm["title"])  #需求名称
                                worksheet.write(x, 1, xm['topics'][i]['title'] + '/' + test_module['topics'][j]['title'])   #模块名称
                                worksheet.write(x, 2, test_suit['topics'][k]['title'])  #用例标题
                                worksheet.write(x, 3, test_case['topics'][m]['title'])  #前提条件
                                worksheet.write(x, 4, test_case_t['topics'][n]['title'])    #操作步骤
                                worksheet.write(x, 5, test_case_q['topics'][t]['title'])    #预期结果
                                x += 1  #每次插入一条数据后增加1，保证每条用例插入位置正确
        workbook.save(xm["title"] + ".xls")  # 保存excel表格，表格名称取xmind主题名称

if __name__ == '__main__':
    # xmind_file_path = 'C:\\Users\Lenovo\Desktop\Xmind转Excel模板.xmind'
    xmind_file_path = 'C:/Users/Lenovo/Desktop/Xmind转Excel模板.xmind'
    XmindToExcelTest().write_excel(xmind_file_path)